<?php

namespace app\api\controller;

use app\common\model\dealer\Capital;
use app\common\model\dealer\User;
use app\common\model\user\PointsLog;
use app\store\model\dealer\Setting as SettingModel;

class  Cron extends \think\Controller
{


    /**
     * 发放分销商佣金
     * @param $user_id
     * @param $money
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function grantMoney($user_id, $money, $parme, $level, $memo = '订单佣金结算')
    {


        $model = \app\api\model\User::detail($user_id);

        // 累积分销商可提现佣金
        $model->setInc('money', $money);


        Capital::add([
            'user_id' => $user_id,
            'flow_type' => 10,
            'money' => $money,
            'describe' => $memo,
            'wxapp_id' => $model['wxapp_id'],
            'yin_id' => $parme['user_id'],
            'order_id' => $parme['order_id'],
            'he_id' => $parme['he_id']
        ]);


    }

//

    public function ceshi()
    {
        $data = \app\store\model\dealer\Setting::getAll();
        $money = db('order')
            ->where('user_id', 10113)
            ->whereTime('pay_time', 'yesterday')
            ->where('pay_status', 20)
            ->field('*,sum(achievement) as yeji')
            ->find();
        print_r($money);
        exit;
        $hexiao1 = $data['achievement']['values']['hexiaoyuan'] * 0.01;
        $dealer_referee = db('dealer_referee')
            ->where('dealer_id', 10113)
            ->count();
        bcmul($money, $hexiao1, 2);

    }

    /**
     * 分红奖励
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function bonus()
    {
        $data = \app\store\model\dealer\Setting::getAll();
        $fenhonbi = $data['achievement']['values']['fenhon'] * 0.01;
        $hexiao1 = $data['achievement']['values']['hexiaoyuan'] * 0.01;

        $user = db('user')->select();
        $money = db('order')
            ->whereTime('pay_time', 'today')
            ->where('pay_status', 20)
            ->sum('achievement');
        if ($money > 0) {
            foreach ($user as $k => $v) {
                $dealer_referee = db('dealer_referee')
                    ->where('dealer_id', $v['user_id'])
                    ->where('level', 1)
                    ->count();
                $hexiao = db('store_shop_clerk')
                    ->where('user_id', $v['user_id'])
                    ->where('is_delete', 0)
                    ->find();
                if ($dealer_referee >= 5) {
                    $this->setmoney($v['user_id'], bcmul($money, $fenhonbi, 2), '邀请达到五人总业绩加权分红奖励', 12, '');
                }
                if ($hexiao) {
                    $this->setmoney($v['user_id'], bcmul($money, $hexiao1, 2), '核销员享受公司业绩分红', 15, '');
                }
            }
        }


    }

    public function setmoney($user_id, $money, $memo, $type, $len = null)
    {
        $data = SettingModel::getAll();
        $model = User::detail($user_id);
        if (!$model || $model['is_delete']) {
            return false;
        }
        $order = db('order')
            ->where('user_id', $user_id)
            ->where('pay_status', 20)
            ->whereIn('goods_type', ['A', 'B', 'C'])
            ->find();
        $hexiao = '';
        if ($len) {
            $order = 3333;
            $hexiao = 11;
        }

        if ($order) {
            $mon = $data['achievement']['values']['jifen'];
            // 累积分销商可提现佣金
            $zhon = $mon * 0.01;
            $jifenzhon = 1 - $zhon;
            $money1 = bcmul($money, $zhon, 1);
            $ponint = bcmul($money, $jifenzhon, 1);


            if ($hexiao) {
                $model->setInc('money', $money);
                $user = db('user')->where('user_id', $user_id)->setInc('dealer_money', $money);
                Capital::add([
                    'user_id' => $user_id,
                    'flow_type' => 10,
                    'money' => $money,
                    'describe' => $memo,
                    'wxapp_id' => $model['wxapp_id'],
                    'yin_id' => 0,
                    'type' => $type,
                    'order_id' => 'S' . date('Y-m-d H:i:s', time()),
                    'he_id' => 'M' . date('Y-m-d H:i:s', time())
                ]);
            } else {
                $model->setInc('money', $money1);
                $user = db('user')->where('user_id', $user_id)->setInc('dealer_money', $money1);
                Capital::add([
                    'user_id' => $user_id,
                    'flow_type' => 10,
                    'money' => $money1,
                    'describe' => $memo,
                    'wxapp_id' => $model['wxapp_id'],
                    'yin_id' => 0,
                    'type' => $type,
                    'order_id' => 'S' . date('Y-m-d H:i:s', time()),
                    'he_id' => 'M' . date('Y-m-d H:i:s', time())
                ]);

                db('user')
                    ->where('user_id', $user_id)
                    ->setInc('points', $ponint);
                PointsLog::add([
                    'user_id' => $user_id,
                    'remark' => $memo,
                    'value' => $ponint,
                    'describe' => $memo,
                    'wxapp_id' => 10001,
                    'create_time' => time(),
                    'type' => $type,
                    'yin_id' => 0,
                    'order_id' => 'S' . date('Y-m-d H:i:s', time()),
                    'he_id' => 'M' . date('Y-m-d H:i:s', time())
                ]);
            }


            return true;
        } else {
            return true;
        }

    }

    /**
     * 业务提成统计
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function business()
    {
        $user = db('user')->select();
        foreach ($user as $k => $v) {
            $user1count = db('dealer_referee')
                ->where('dealer_id', $v['user_id'])
                ->where('level', 1)
                ->count();

            if ($user1count > 1) {
                $user1 = db('dealer_referee')
                    ->where('dealer_id', $v['user_id'])
                    ->where('level', 1)
                    ->select();

                $useryijiid = [];
                $usererjiid = [];
                foreach ($user1 as $kk => $vv) {
                    $useryijiid[] = $vv['dealer_id'];
                }

                foreach ($useryijiid as $kv => $vk) {
                    $usererjiid[] = db('dealer_referee')
                        ->where('dealer_id', $vk['user_id'])
                        ->where('level', 2)->value('user_id');
                }

            } else {


            }

        }

    }

    /**
     * 业务提成 返利
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function busine()
    {


    }

}